package com.castle.fortress.admin.study.exam.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.castle.fortress.admin.core.constants.GlobalConstants;
import com.castle.fortress.admin.study.exam.dto.StudyExamStatisticsMemberDto;
import com.castle.fortress.admin.study.exam.entity.StudyMemberExamEntity;
import com.castle.fortress.admin.study.exam.dto.StudyMemberExamDto;
import com.castle.fortress.admin.study.exam.service.StudyExamStatisticsMemberService;
import com.castle.fortress.admin.study.exam.service.StudyMemberExamService;
import com.castle.fortress.common.utils.ConvertUtil;
import com.castle.fortress.common.entity.RespBody;
import com.castle.fortress.common.exception.BizException;
import com.castle.fortress.common.respcode.GlobalRespCode;
import com.castle.fortress.common.enums.OperationTypeEnum;
import com.castle.fortress.admin.core.annotation.CastleLog;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;
import com.castle.fortress.admin.utils.ExcelUtils;
import javax.servlet.http.HttpServletResponse;
import com.castle.fortress.common.entity.DynamicExcelEntity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户试卷 控制器
 *
 * @author castle
 * @since 2023-02-16
 */
@Api(tags="用户试卷管理控制器")
@Controller
public class StudyMemberExamController {
    @Autowired
    private StudyMemberExamService studyMemberExamService;
    @Autowired
    private StudyExamStatisticsMemberService studyExamStatisticsMemberService;

    /**
     * 用户试卷的分页展示
     * @param studyMemberExamDto 用户试卷实体类
     * @param current 当前页
     * @param size  每页记录数
     * @return
     */
    @CastleLog(operLocation="用户试卷",operType = OperationTypeEnum.QUERY)
    @ApiOperation("用户试卷分页展示")
    @GetMapping("/study/exam/studyMemberExam/page")
    @ResponseBody
    @RequiresPermissions("study:exam:studyMemberExam:pageList")
    public RespBody<IPage<StudyMemberExamDto>> pageStudyMemberExam(StudyMemberExamDto studyMemberExamDto, @RequestParam(required = false) Integer current, @RequestParam(required = false)Integer size){
        Integer pageIndex= current==null? GlobalConstants.DEFAULT_PAGE_INDEX:current;
        Integer pageSize= size==null? GlobalConstants.DEFAULT_PAGE_SIZE:size;
        Page<StudyMemberExamDto> page = new Page(pageIndex, pageSize);
        IPage<StudyMemberExamDto> pages = studyMemberExamService.pageStudyMemberExamExtends(page, studyMemberExamDto);

        return RespBody.data(pages);
    }

    /**
     * 用户试卷的列表展示
     * @param studyMemberExamDto 用户试卷实体类
     * @return
     */
    @CastleLog(operLocation="用户试卷",operType = OperationTypeEnum.QUERY)
    @ApiOperation("用户试卷列表展示")
    @GetMapping("/study/exam/studyMemberExam/list")
    @ResponseBody
    public RespBody<List<StudyMemberExamDto>> listStudyMemberExam(StudyMemberExamDto studyMemberExamDto){
        List<StudyMemberExamDto> list = studyMemberExamService.listStudyMemberExam(studyMemberExamDto);
        return RespBody.data(list);
    }

    /**
     * 用户试卷保存
     * @param studyMemberExamDto 用户试卷实体类
     * @return
     */
    @CastleLog(operLocation="用户试卷",operType = OperationTypeEnum.INSERT)
    @ApiOperation("用户试卷保存")
    @PostMapping("/study/exam/studyMemberExam/save")
    @ResponseBody
    @RequiresPermissions("study:exam:studyMemberExam:save")
    public RespBody<String> saveStudyMemberExam(@RequestBody StudyMemberExamDto studyMemberExamDto){
        if(studyMemberExamDto == null ){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
        StudyMemberExamEntity studyMemberExamEntity = ConvertUtil.transformObj(studyMemberExamDto,StudyMemberExamEntity.class);
        if(studyMemberExamService.save(studyMemberExamEntity)){
            return RespBody.data("保存成功");
        }else{
            return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
        }
    }

    /**
     * 用户试卷编辑
     * @param studyMemberExamDto 用户试卷实体类
     * @return
     */
    @CastleLog(operLocation="用户试卷",operType = OperationTypeEnum.UPDATE)
    @ApiOperation("用户试卷编辑")
    @PostMapping("/study/exam/studyMemberExam/edit")
    @ResponseBody
    @RequiresPermissions("study:exam:studyMemberExam:edit")
    public RespBody<String> updateStudyMemberExam(@RequestBody StudyMemberExamDto studyMemberExamDto){
        if(studyMemberExamDto == null || studyMemberExamDto.getId() == null || studyMemberExamDto.getId().equals(0L)){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
        StudyMemberExamEntity studyMemberExamEntity = ConvertUtil.transformObj(studyMemberExamDto,StudyMemberExamEntity.class);
        if(studyMemberExamService.updateById(studyMemberExamEntity)){
            return RespBody.data("保存成功");
        }else{
            return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
        }
    }

    /**
     * 用户试卷删除
     * @param id
     * @return
     */
    @CastleLog(operLocation="用户试卷",operType = OperationTypeEnum.DELETE)
    @ApiOperation("用户试卷删除")
    @PostMapping("/study/exam/studyMemberExam/delete")
    @ResponseBody
    @RequiresPermissions("study:exam:studyMemberExam:delete")
    public RespBody<String> deleteStudyMemberExam(@RequestParam Long id){
        if(id == null ){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
        if(studyMemberExamService.removeById(id)) {
            return RespBody.data("保存成功");
        }else{
            return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
        }
    }


    /**
     * 用户试卷批量删除
     * @param ids
     * @return
     */
    @CastleLog(operLocation="用户试卷",operType = OperationTypeEnum.DELETE)
    @ApiOperation("用户试卷批量删除")
    @PostMapping("/study/exam/studyMemberExam/deleteBatch")
    @ResponseBody
    @RequiresPermissions("study:exam:studyMemberExam:deleteBatch")
    public RespBody<String> deleteStudyMemberExamBatch(@RequestBody List<Long> ids){
        if(ids == null || ids.size()<1){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
        if(studyMemberExamService.removeByIds(ids)) {
            return RespBody.data("保存成功");
        }else{
            return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
        }
    }
    /**
     * 用户考试试卷详情
     * @param recordId 用户考试记录ID
     * @return
     */
    @CastleLog(operLocation="用户考试试卷详情",operType = OperationTypeEnum.QUERY)
    @ApiOperation("用户考试试卷详情")
    @GetMapping("/study/exam/studyMemberExam/detail")
    @ResponseBody
    @RequiresPermissions("study:exam:studyMemberExam:detail")
    public RespBody<Map<String,Object>> detailStudyMemberExam(@RequestParam Long recordId){
        if(recordId == null){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
        Map<String,Object> resultMap = new HashMap<>();
        StudyMemberExamDto studyMemberExamDto = new StudyMemberExamDto();
        studyMemberExamDto.setRecordId(recordId);
        List<StudyMemberExamDto> questions =  studyMemberExamService.listStudyMemberExam(studyMemberExamDto);
        resultMap.put("questions",questions);
        StudyExamStatisticsMemberDto examStatisticsMemberDto = studyExamStatisticsMemberService.queryStatisticeMemberDetail(recordId);
        resultMap.put("examName",examStatisticsMemberDto.getExamName());
        resultMap.put("memberName",examStatisticsMemberDto.getMemberName());
        resultMap.put("score",examStatisticsMemberDto.getScore());
        return RespBody.data(resultMap);
    }

    /**
     * 用户试卷详情
     * @param id 用户试卷id
     * @return
     */
    @CastleLog(operLocation="用户试卷",operType = OperationTypeEnum.QUERY)
    @ApiOperation("用户试卷详情")
    @GetMapping("/study/exam/studyMemberExam/info")
    @ResponseBody
    @RequiresPermissions("study:exam:studyMemberExam:info")
    public RespBody<StudyMemberExamDto> infoStudyMemberExam(@RequestParam Long id){
        if(id == null){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
        StudyMemberExamDto studyMemberExamDto =  studyMemberExamService.getByIdExtends(id);

        return RespBody.data(studyMemberExamDto);
    }

	/**
     * 动态表头导出 依据展示的字段导出对应报表
     * @param dynamicExcelEntity
     * @param response
     * @throws Exception
     */
    @CastleLog(operLocation="用户试卷",operType = OperationTypeEnum.EXPORT)
	@PostMapping("/study/exam/studyMemberExam/exportDynamic")
	@ApiOperation("动态表头导出，依据展示的字段导出对应报表")
	public void exportDynamic(@RequestBody DynamicExcelEntity<StudyMemberExamDto> dynamicExcelEntity, HttpServletResponse response) throws Exception {
		List<StudyMemberExamDto> list = studyMemberExamService.listStudyMemberExam(dynamicExcelEntity.getDto());
		//字典、枚举、接口、json常量等转义后的列表数据 根据实际情况初始化该对象，null为list数据直接导出
		List<List<Object>> dataList = null;
		/**
        * 根据实际情况初始化dataList,可参照 com.castle.fortress.admin.system.controller.TmpDemoController类中方法：exportDynamic
        */
        List<Map<String,String>> headerList = initHeaderList();
		ExcelUtils.exportDynamic(response,dynamicExcelEntity.getFileName()+".xlsx",null,list,headerList,dataList);
	}

    private List<Map<String,String>> initHeaderList(){
        List<Map<String,String>> headerList = new ArrayList<>();
        Map<String,String> sortMap = new HashMap<>();
        sortMap.put("name","题目排序");
        sortMap.put("code","sort");
        headerList.add(sortMap);
        Map<String,String> titleMap = new HashMap<>();
        titleMap.put("name","标题");
        titleMap.put("code","title");
        headerList.add(titleMap);
        Map<String,String> questionTypeMap = new HashMap<>();
        questionTypeMap.put("name","题目类型");
        questionTypeMap.put("code","questionType");
        headerList.add(questionTypeMap);
        Map<String,String> scoreMap = new HashMap<>();
        scoreMap.put("name","题目分值");
        scoreMap.put("code","score");
        headerList.add(scoreMap);
        Map<String,String> answerAMap = new HashMap<>();
        answerAMap.put("name","选项A内容");
        answerAMap.put("code","answerA");
        headerList.add(answerAMap);
        Map<String,String> answerBMap = new HashMap<>();
        answerBMap.put("name","选项B内容");
        answerBMap.put("code","answerB");
        headerList.add(answerBMap);
        Map<String,String> answerCMap = new HashMap<>();
        answerCMap.put("name","选项C内容");
        answerCMap.put("code","answerC");
        headerList.add(answerCMap);
        Map<String,String> answerDMap = new HashMap<>();
        answerDMap.put("name","选项D内容");
        answerDMap.put("code","answerD");
        headerList.add(answerDMap);
        Map<String,String> answerEMap = new HashMap<>();
        answerEMap.put("name","选项E内容");
        answerEMap.put("code","answerE");
        headerList.add(answerEMap);
        Map<String,String> answerFMap = new HashMap<>();
        answerFMap.put("name","选项F内容");
        answerFMap.put("code","answerF");
        headerList.add(answerFMap);
        Map<String,String> answerMap = new HashMap<>();
        answerMap.put("name","答案");
        answerMap.put("code","answer");
        headerList.add(answerMap);
        Map<String,String> choiceAnswerMap = new HashMap<>();
        choiceAnswerMap.put("name","用户选择的答案");
        choiceAnswerMap.put("code","choiceAnswer");
        headerList.add(choiceAnswerMap);
        Map<String,String> analysisMap = new HashMap<>();
        analysisMap.put("name","题目解析");
        analysisMap.put("code","analysis");
        headerList.add(analysisMap);
        return headerList;
    }


}
